#!/usr/bin/env bash


help(){
    cat<<__EOF__
    usage:  m vpn [ ls | list | start | stop | status | help ]

    Examples:
      m vpn ls                              # list VPN connections

      m vpn start VPN                       # start vpn connection named VPN
      m vpn start VPN USER                  # start a vpn connection with a given user
      m vpn start VPN USER PASS             # start a vpn connection with a given user and password
      m vpn start VPN USER PASS SECRET      # start a vpn connection with a given user,password, and secret

      m vpn stop VPN                        # stop vpn connection named VPN
      m vpn status VPN                      # status vpn connection named VPN
__EOF__

}

list_vpns(){
    scutil --nc list
}

start_vpn(){
    [ -z "$1" ] && help && exit 1;

    if [ $# -eq 4 ]; then
        # vpn user pass secret
        scutil --nc start "$1" --user "$2" --password "$3" --secret "$4"
    elif [ $# -eq 3 ]; then
        # vpn user password
        scutil --nc start "$1" --user "$2" --password "$3"
    elif [ $# -eq 2 ]; then
        # vpn user
        scutil --nc start "$1" --user "$2"
    else
        # vpn
        scutil --nc start "$1"
    fi
}

stop_vpn(){
    scutil --nc stop $1
}

vpn_status(){
    scutil --nc status $1
}


case $1 in
    help)
        help
        ;;
    ls|list)
        list_vpns
        ;;
    start)
        shift
        start_vpn "$@"
        ;;
    stop)
        [ -z "$2" ] && help && exit 1;
        stop_vpn $2
        ;;
    status)
        [ -z "$2" ] && help && exit 1;
        vpn_status $2
        ;;
    *)
        help
        ;;
esac

# vim: ts=4 sw=4 softtabstop=4 expandtab
